﻿package  
{
import flash.display.*;
import flash.events.*;

public class KeyboardListener 
{		
	private const UP_ARROW_CODE : int = 38;
	private const DOWN_ARROW_CODE : int = 40;
	private const LEFT_ARROW_CODE : int = 37;
	private const RIGHT_ARROW_CODE : int = 39;
	private const SPACE_BAR_CODE : int = 32;
		
	public var up : Boolean = false;
	public var down : Boolean = false;
	public var left : Boolean = false;
	public var right  : Boolean = false;
	public var spacebar : Boolean = false;
	
	private var m_stage : Stage;	
	
	public function KeyboardListener( a_stage : Stage ) 
	{
		m_stage = a_stage;
		
		m_stage.addEventListener( KeyboardEvent.KEY_UP, processKeyUp );
		m_stage.addEventListener( KeyboardEvent.KEY_DOWN, processKeyDown );
	}	
	
	private function processKeyUp( a_event : KeyboardEvent ) : void
	{
		switch( a_event.keyCode ) 
		{
			case UP_ARROW_CODE:
			up = false;
			break;
			case DOWN_ARROW_CODE:
			down = false;
			break;
			case LEFT_ARROW_CODE:
			left = false;
			break;
			case RIGHT_ARROW_CODE:
			right = false;
			break;
			case SPACE_BAR_CODE:
			spacebar = false;
			break;
		}		
	}
	
	private function processKeyDown( a_event : KeyboardEvent ) : void
	{		
		switch( a_event.keyCode ) 
		{
			case UP_ARROW_CODE:
			up = true;
			break;
			case DOWN_ARROW_CODE:
			down = true;
			break;
			case LEFT_ARROW_CODE:
			left = true;
			break;
			case RIGHT_ARROW_CODE:
			right = true;
			break;
			case SPACE_BAR_CODE:
			spacebar = true;
			break;			
		}
	}
	
}	
}